package cz.cvut.fit.mi.dpo.mvc.view.tables;

import cz.cvut.fit.mi.dpo.mvc.controller.Controller;
import cz.cvut.fit.mi.dpo.mvc.model.IReadable;
import cz.cvut.fit.mi.dpo.mvc.model.elements.AElement;
import cz.cvut.fit.mi.dpo.mvc.model.elements.Circle;
import cz.cvut.fit.mi.dpo.mvc.model.elements.ElementCollection;

/**
 * @author David Pertl
 * @author Martin Štekl <steklmar@fit.cvut.cz>
 */
public class CircleTable extends ATable {

	public CircleTable(Controller controller, IReadable model, String name) {
		super(controller, model, name);
		table.setModel(new Model(new String[]{"id", "X", "Y", "Radius"}));
	}

	private class Model extends AModel {

		private Model(String[] columnNames) {
			super(columnNames);
		}

		@Override
		protected ElementCollection<? extends AElement> getModel() {
			return model.getCircles();
		}

		@Override
		protected Object getValueFrom(AElement element, int columnIndex) {
			Circle circle = (Circle) element;
			switch (columnIndex) {
				case 3:
					return circle.getRadius();
			}
			return super.getValueFrom(element, columnIndex);
		}

		@Override
		protected void setValueFor(AElement element, int columnIndex, int value) {
			Circle circle = (Circle) element;
			switch (columnIndex) {
				case 1:
					controller.editCircle(circle.getId(), value, circle.getY(), circle.getRadius());
					break;
				case 2:
					controller.editCircle(circle.getId(), circle.getX(), value, circle.getRadius());
					break;
				case 3:
					controller.editCircle(circle.getId(), circle.getX(), circle.getY(), value);
					break;
				default:
					throw new IllegalArgumentException("Unknown column index [" + columnIndex + "].");
			}
		}

	}

}
